<?php
import('$/util/model_type.php');

class TalkHelper
{
	/**
	 * 添加一条指定模型的谈论
	 *
	 */
	public static function addFor($model_type, $model_id, $uid, $content, $title=null)
	{
		return Talk::add(array(
				'model_type'	=>	$model_type,
				'model_id'		=>	$model_id,
				'uid'			=>	$uid,
				'addtime'		=>	time(),
				'content'		=>	strip_tags($content),
				'title'			=>	trim(strip_tags($title)),
				));
	}
	/**
	 * 取对指定模型的谈论列表
	 */
	public static function listFor($model_type, $model_id, $s=0, $l=0)
	{
		return Db::get('Talk')->select()->where(array(
				'model_type'	=>	$model_type,
				'model_id'		=>	$model_id,
				'off'			=>	0,
				))->order('tid ASC')->limit($s, $l)->getAll();
	}
	public static function countFor($model_type, $model_id)
	{
		return Db::get('Talk')->count()->where(array(
				'model_type'	=>	$model_type,
				'model_id'		=>	$model_id,
				'off'			=>	0,
				))->getCol();
	}
	
	/**
	 * 返回用户指定model_type下所有talks信息
	 *
	 * @param $holder 每个model_id只显示一条?
	 */
	public static function listForUser($model_type, $uid, $holder=true, $limit=10)
	{
		if($holder)
		{
			return Db::get('Talk')->select('tid,model_id,addtime,title,content')->where(array(
					'model_type'	=>	$model_type,
					'uid'			=>	$uid,
					'off'			=>	0,
					))->group('model_id')->order('tid DESC')->limit($limit)->getAll();
		}
		else
		{
			return Db::get('Talk')->select('tid,model_id,addtime,title,content')->where(array(
					'model_type'	=>	$model_type,
					'uid'			=>	$uid,
					'off'			=>	0,
					))->order('tid DESC')->limit($limit)->getAll();
		}
	}
	
}
?>